package com.sinoma.G2project.config.datasource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
@Order(0)
public class DataSourceAspect {
    @Pointcut("execution (* com.sinoma.G2project.MoYu.Service..*(..))")
    public void aspect(){

    }
    @Before("aspect()")
    public void before(JoinPoint joinPoint){
        Class<?> clazz = joinPoint.getTarget().getClass();
        Method[] method = clazz.getMethods();
        DataSwitch dataswitch = null;
        if (clazz.isAnnotationPresent(DataSwitch.class)){
            dataswitch = clazz.getAnnotation(DataSwitch.class);
            DynamicDataSourceHolder.setThreadLocal(dataswitch.value());
        } else{
            DynamicDataSourceHolder.setThreadLocal("Member");
        }
    }

    @After("aspect()")
    public void after(){
        DynamicDataSourceHolder.clear();
    }
}
